package com.yuelan.codelib.utils;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;

public class AnimUtil {
	public static void TbScollAnim(final View view1, final View view2,
			final long betime, final boolean canfor) {
		if (view1.getVisibility() == View.GONE) {
			view1.setVisibility(View.VISIBLE);
		}
		if (view2.getVisibility() == View.GONE) {
			view2.setVisibility(View.VISIBLE);
		}
		final TranslateAnimation IN = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		final TranslateAnimation OUT = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, -1.0f);
		IN.setStartTime(0);
		IN.setDuration(betime);
		// IN.setFillAfter(true);
		// IN.setRepeatCount(Integer.MAX_VALUE);
		// IN.setRepeatMode(Animation.REVERSE);
		OUT.setStartTime(0);
		OUT.setDuration(betime);
		OUT.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				view1.setVisibility(View.GONE);

			}
		});
		// OUT.setFillAfter(true);
		// OUT.setRepeatCount(Integer.MAX_VALUE);
		// OUT.setRepeatMode(Animation.REVERSE);
		view1.startAnimation(OUT);
		view2.startAnimation(IN);
		if (canfor) {
			final Handler handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					TbScollAnim(view2, view1, betime, canfor);
					super.handleMessage(msg);
				}
			};
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						Thread.sleep(betime + 1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					handler.sendEmptyMessage(1);
				}
			}).start();
		}
	}

}
